﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace backoffice
{
    public partial class productForm : Form
    {
        int id; // id du produit
        int category_id; // id de la catégorie dans le cas d'un ajout

        public productForm(int id, int category_id)
        {
            InitializeComponent();
            this.id = 0;
            this.category_id = category_id;
        }

        public productForm(int id)
        {
            InitializeComponent();
            this.id = id;
        }

        private void productForm_Load(object sender, EventArgs e)
        {
            if (!id.Equals(0))
                populateAndBind();
        }

        /**
         * 
         * Remplit les input dans le cas d'un edit
         * 
         */
        private void populateAndBind()
        {
            BU.ProductManager manager = new BU.ProductManager();
            BU.Product product = manager.getOne(id);

            tbName.Text = product.Name;
            tbDescription.Text = product.Description;
            tbPrice.Text = product.Price.ToString();
            tbStock.Text = product.Stock.ToString();
            this.id = product.ID;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            
            saveProduct();
        }

        /**
         * 
         * Sauvegarde le produit
         * 
         */
        private void saveProduct()
        {
            BU.ProductManager manager = new BU.ProductManager();
            if (!tbName.Text.Equals("") && !tbDescription.Text.Equals("") && !tbPrice.Text.Equals("") && !tbStock.Text.Equals(""))
                if (id.Equals(0))
                    manager.save(0, tbName.Text, tbDescription.Text, tbPrice.Text, tbStock.Text, this.category_id);
                else
                    manager.save(this.id, tbName.Text, tbDescription.Text, tbPrice.Text, tbStock.Text, 0);

            DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
